package com.xiaobai.netease_news.fragments;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.VideoView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.xiaobai.netease_news.R;
import com.xiaobai.netease_news.adapters.VideoAdapter;
import com.xiaobai.netease_news.constants.Constant;
import com.xiaobai.netease_news.thread.VideoAsyncTask;


public class VideoFragment extends Fragment
        implements PullToRefreshBase.OnRefreshListener2<ListView>, AdapterView.OnItemClickListener, AbsListView.OnScrollListener {


    private VideoAdapter videoAdapter;
    private PullToRefreshListView ptrListView;
    private int currentCount = 10;
    private final String VIDEOURL = "http://c.m.163.com/nc/video/list/V9LG4B3A0/n/%s-10.html";
    ;
    private VideoView videoView;
    private ProgressBar progressBar;


    public VideoFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_video, container, false);

        //下拉刷新控件
        ptrListView = (PullToRefreshListView) view.findViewById(R.id.fragment_video_ptr_list);
        ListView listView = ptrListView.getRefreshableView();


        //初始化Adapter
        videoAdapter = new VideoAdapter(getActivity(), null, R.layout.item_video);
        listView.setAdapter(videoAdapter);
        listView.setFocusable(true);
        //设置单条点击监听事件
        listView.setOnItemClickListener(this);
        //设置滑动监听
        listView.setOnScrollListener(this);

        //设置下拉上拉方式，并绑定监听
        ptrListView.setMode(PullToRefreshBase.Mode.BOTH);
        ptrListView.setOnRefreshListener(this);

        //启动异步任务加载初始数据
        String url = String.format(VIDEOURL, "0");
        Log.d("VideoFragment", "url:" + url);
        new VideoAsyncTask(getActivity(), videoAdapter, ptrListView).execute(url);

        return view;
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    /**
     * 下拉刷新
     *
     * @param refreshView
     */
    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        String url = String.format(VIDEOURL, "0");
        new VideoAsyncTask(getActivity(), videoAdapter, ptrListView).execute(url, Constant.PULLDOWN);
    }

    /**
     * 上拉刷新
     *
     * @param refreshView
     */
    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        String url = String.format(VIDEOURL, currentCount + "");
        new VideoAsyncTask(getActivity(), videoAdapter, ptrListView).execute(url, Constant.PULLUP);
        currentCount += 10;
    }


    //ListView 条目点击处理（无效）
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.d("VideoFragment", "id : " + id);
        Log.d("VideoFragment", "position : " + position);
    }

    /**
     * 滑动状态改变
     *
     * @param view
     * @param scrollState
     */
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
//        AbsListView.OnScrollListener.SCROLL_STATE_FLING;    //
//        AbsListView.OnScrollListener.SCROLL_STATE_IDLE;    //
//        AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL;    //

        switch(scrollState){
            case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                Log.d("VideoFragment","onScrollStateChanged0:"+scrollState);
                break;
            case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                Log.d("VideoFragment","onScrollStateChanged1:"+scrollState);
                break;
            case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
                Log.d("VideoFragment","onScrollStateChanged2:"+scrollState);
                break;
        }

    }

    /**
     * 滑动时
     *
     * @param view
     * @param firstVisibleItem
     * @param visibleItemCount
     * @param totalItemCount
     */
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        Log.d("VideoFragment","onScroll : " + firstVisibleItem);
        Log.d("VideoFragment","onScroll : " + totalItemCount);
    }
}
